<script type="text/javascript">
    function _add_more(type) {
        if (type == 1){ // debet
            var index = document.getElementsByName('akun_debet[]');
            var txt = "<p><label>Akun</label>"+
                "<select name=\"akun_debet[]\" id=\"akun_debet"+index.length+"\"><option value=\"0\">-- Pilih Akun --</option></select>"+
                " </p> <p><label>Jumlah</label><input name=\"jml_debet[]\" id=\"jml_debet"+index.length+"\" value=\"\" > </p>";

            document.getElementById("debetField").innerHTML += txt;
        }else if (type == 2){ // kredit
            var index = document.getElementsByName('akun_kredit[]');
            var txt = "<p><label>Akun</label>"+
                "<select name=\"akun_kredit[]\" id=\"akun_kredit"+index.length+"\"><option value=\"0\">-- Pilih Akun --</option></select>"+
                " </p> <p><label>Jumlah</label><input name=\"jml_kredit[]\" id=\"jml_kredit"+index.length+"\" value=\"\" > </p>";

            document.getElementById("kreditField").innerHTML += txt;
        }

        loadinduk(type);
    }

    function loadinduk(type) {
        if (type == 1)
            var count = document.getElementsByName('akun_debet[]');
        else if (type == 2)
            var count = document.getElementsByName('akun_kredit[]');

        $.ajax({
           url: "<?=base_url();?>index.php/keuangan/transaksi/getAkun/",
           global: false,
           type: "POST",
           async: false,
           dataType: "html",
           success: function (response) //'response' is the output provided by the controller method prova()
                {
                    //counts the number of dynamically generated options
                    var dynamic_options = $("*").index( $('.dynamic4')[0] );
                    //removes previously dynamically generated options if they exists (not equal to 0)
                    if ( dynamic_options != (-1)) $(".dynamic4").remove();

                    for (i = 0; i < count.length; i++){
                        if (type == 1)
                            $("select#akun_debet"+i).append(response);
                        else if (type == 2)
                            $("select#akun_kredit"+i).append(response);
                    }

                $(".selected").attr({selected: ' selected'});
               }

          });

          return false;
    }
    
    function hitungBalance(){
        // hitung total jumlah debet
        var count = document.getElementsByName('jml_debet[]');
        var total = Number(0);

        for (i = 0; i < count.length; i++){
            var val = (document.getElementById('jml_debet'+i)).value;
            if (val == NaN || val == '')
                val = 0;
            var nilai = parseInt(val);
            total += nilai;
        }
        document.form_edit_transaksi.total_debet.value = Number(total);

        // hitung total jumlah kredit
        var count = document.getElementsByName('jml_kredit[]');
        var total = Number(0);

        for (i = 0; i < count.length; i++){
            var val = (document.getElementById('jml_kredit'+i)).value;
            if (val == NaN || val == '')
                val = 0;
            var nilai = parseInt(val);
            total += nilai;
        }
        document.form_edit_transaksi.total_kredit.value = Number(total);
    }
</script>

<div id="navigation2" style="display:none;">
	<ul>
        <li class="selected">
           <a href="#">Transaksi</a>
        </li>
        <li>
           <a href="#">Debit</a>
        </li>
        <li>
           <a href="#">Kredit</a>
        </li>
        <li>
           <a href="#">Balance</a>
        </li>
    </ul>
</div>

<div id="s" class="form complex">
<?php
echo form_open($post_action, array('name'=>'form_edit_transaksi'));
?>
    <fieldset class="" style="width:810px;">
            <legend style="width:810px;">Transaksi</legend>
            <? foreach ($transaksi as $row){?>
            <p>
                    <label>Tanggal</label>
                    <input style="width: 30px;margin-right:5px;" id="element_4_2" name="dd_transaksi" size="2" maxlength="2" value="<? if (set_value('dd_transaksi') != '') echo set_value('dd_transaksi'); else echo substr($row->TANGGAL_TRANSAKSI, 8, 2);?>" type="text">
                    <input style="width:30px;margin-right:5px;" id="element_4_1" name="mm_transaksi" size="2" maxlength="2" value="<? if (set_value('mm_transaksi') != '') echo set_value('mm_transaksi'); else echo substr($row->TANGGAL_TRANSAKSI, 5, 2);?>" type="text">
                    <input style="width: 80px;" id="element_4_3" name="yy_transaksi" size="4" maxlength="4" value="<? if (set_value('yy_transaksi') != '') echo set_value('yy_transaksi'); else echo substr($row->TANGGAL_TRANSAKSI, 0, 4);?>" type="text">

                    <img id="cal_img_4" src="<?=base_url();?>images/form/calendar.gif" alt="Pick a date.">

                    <script type="text/javascript">
                            Calendar.setup({
                            inputField	 : "element_4_3",
                            baseField    : "element_4",
                            displayArea  : "calendar_4",
                            button		 : "cal_img_4",
                            ifFormat	 : "%B %e, %Y",
                            onSelect	 : selectDate
                            });

                    </script>
            </p>

            <p>
                    <label>Keterangan</label>
                    <textarea id="keterangan" name="keterangan"><?=$row->KETERANGAN?></textarea>
            </p>
            <?} ?>
    </fieldset>
    <fieldset class="" id="debetField" style="width:810px;">
            <legend  style="width:810px;">Debit</legend>
            <p>
                <a href="javascript:_add_more(1);" title="tambah akun">add</a>
            </p>
            <? $no = 0; foreach ($debet as $row){?>
            <p>
            <label>Akun</label>
                    <?
                    if (set_value('akun_debet')!='') $select = set_value('akun_debet'); else $select = $row->ID_AKUN;
                    //echo form_dropdown('akun_debet[]', $akun, $select); ?>
                <select name="akun_debet[]" id="akun_debet<?=$no?>">
                    <?
                        foreach ($akun as $key_nomer=>$value_nomer){
                            
                            echo '<option value="'.$key_nomer.'" '.($row->ID_AKUN==$key_nomer?'selected':'').'>'.$value_nomer.'</option>';
                        }
                    ?>
                </select>
            </p>
            <?php echo form_error('akun_debet'); ?>
            <p>
                    <label>Jumlah</label>
                    <input id="jml_debet<?=$no?>" name="jml_debet[]" type="text" value="<?=$row->JUMLAH_TRANSAKSI?>" size="8" tabindex="1" />
            </p>
            <?php echo form_error('jml_debet'); ?>
            <?$no++; }?>
    </fieldset>
    <fieldset class="" id="kreditField"  style="width:810px;">
            <legend style="width:810px;">Kredit</legend>
            <p>
                <a href="javascript:_add_more(2);" title="tambah akun">add</a>
            </p>
            <? $no = 0; foreach ($kredit as $row){?>
            <p>
                    <label>Akun</label>
                    <?
                    if (set_value('akun_kredit')!='') $select = set_value('akun_kredit'); else $select = $row->ID_AKUN;
                    //echo form_dropdown('akun_kredit[]', $akun, $select); ?>

                <select name="akun_kredit[]" id="akun_kredit<?=$no?>">
                    <?
                        foreach ($akun as $key_nomer=>$value_nomer){

                            echo '<option value="'.$key_nomer.'" '.($row->ID_AKUN==$key_nomer?'selected':'').'>'.$value_nomer.'</option>';
                        }
                    ?>
                </select>
            </p>
            <?php echo form_error('akun_kredit'); ?>
            <p>
                    <label class="description">Jumlah</label>
                    <input id="jml_kredit<?=$no?>" name="jml_kredit[]" type="text" value="<?=$row->JUMLAH_TRANSAKSI?>" size="8" tabindex="1" />
            </p>
            <?php echo form_error('jml_kredit'); ?>
            <? $no++; }?>
    </fieldset>
    
    <fieldset class="" style="width:810px;">
        <legend style="width:810px;">Balance</legend>
        <p class="submit">
            <button name="hitung" type="button" onclick="hitungBalance()">Hitung Balance</button>
        </p>
        <?php echo form_error('total_debet'); ?>
        <p>
            <label>Total Debet</label>
            <input id="total_debet" name="total_debet" type="text" value="<?echo set_value('total_debet');?>" size="8" readonly />
        </p>
        <p>
            <label>Total Kredit</label>
            <input id="total_kredit" name="total_kredit" type="text" value="<?echo set_value('total_kredit');?>" size="8" readonly />
        </p>
        <p class="submit">
                <button id="registerButton" type="submit">Simpan</button>&nbsp;
                <button id="registerButton" type="reset">Reset</button>&nbsp;
                <button id="registerButton" type="button" onclick="window.location.href='<?echo site_url("keuangan/transaksi");?>'">Kembali</button>
        </p >
    </fieldset>
<?php
echo form_close();
?>
</div>